#include "YmxVideoController.h"
#include "Crc32.h"


YmxVideoController::YmxVideoController(const ExtendDeviceType& deviceType, const std::string& deviceActknowlege): mchannel{mcommunicator}//VideoController(channel, deviceType, deviceActknowlege)
{   IpAddressInfo addressInfo{"255.255.255.255", 51000};
    mcommunicator.connect(addressInfo);
}



bool YmxVideoController::configAndPlay(VideoPlayInfo& videoPlayInfo) {
    bool result = true;
    result &= setPlayMode(videoPlayInfo);
    result &= setBright(videoPlayInfo);
    result &= setSpeed(videoPlayInfo);
    result &= play(videoPlayInfo);

    mvideoPlayInfo = videoPlayInfo;
    return result;
}


bool YmxVideoController::play(VideoPlayInfo& videoPlayInfo) {
    YmxCommandInfo commandInfo;
    commandInfo.command = 1;
    commandInfo.videoId = std::stoi(videoPlayInfo.videoId);
    commandInfo.crc = 0x9F75A96E;
 //   commandInfo.crc = Crc32::generateCrc32(commandInfo.crc, reinterpret_cast <uint8_t*>(&commandInfo), sizeof commandInfo - sizeof commandInfo.crc);
    return mchannel.sendRaw(&commandInfo, sizeof commandInfo);
}



bool YmxVideoController::setSpeed(VideoPlayInfo& videoPlayInfo) {

    return true;
}




bool YmxVideoController::setBright(VideoPlayInfo& videoPlayInfo) {
    return true;
}



bool YmxVideoController::setPlayMode(VideoPlayInfo& videoPlayInfo) {

    return true;
}



std::string& YmxVideoController::getVideoCount(std::string& result) {

    return result;
}



int32_t YmxVideoController::getVideoDuration(VideoPlayInfo& videoPlayInfo) {
    std::string command{"<AT_CHECK0_VIDEOTIME_>"};
    command.append(videoPlayInfo.videoId);
    command.append(1, '>');
    std::string result;

    std::size_t length = result.size();
    int32_t duration = 0;
    if (0 == result.compare(length - 4, 4, "_OK>")) {
        duration = std::atoi(&result[11]);
    }

    return duration;
}



bool YmxVideoController::setVideoTime(std::string& datetime, VideoPlayInfo& videoPlayInfo) {

    return true;
}



std::string& YmxVideoController::retrieveControllerTime(std::string& result) {
    return result;
}



